export class MathUtils {

    /**
     * 随机种子
     */
    public static seed: number = 10;

    /**
     * 唯一记录
     */
    private static onlyMap: Map<number, boolean> = new Map<number, boolean>();

    /**
     * 随机
     * @param min 
     * @param max 
     */
    static random(min: number, max: number): number {
        this.seed = (this.seed * 9301 + 49297) % 233280;
        let c: number = this.seed / (233280.0);
        return min + c * (max - min);
    }

    /**
     * 随机整数值
     * @param min 
     * @param max 
     */
    static randomInt(min: number, max: number): number {
        return Math.floor(this.random(min, max + 0.999));
    }

    /**
     * 随机唯一稳定值
     * @param min 
     * @param max 
     * @param clear  是否清除唯一记录 
     */
    static randomOnlyInt(min: number, max: number, clear?: boolean): number {
        if (clear) {
            this.onlyMap.clear();
        }
        let value: number = this.randomInt(min, max);
        let tryCount: number = (max - min) * 2;
        let tryIndex: number = 0;
        while (this.onlyMap.has(value)) {
            value = this.randomInt(min, max);
            if (tryIndex > tryCount) {
                console.error("所有唯一值已取完");
                return 0;
            }
            tryIndex++;
        }
        this.onlyMap.set(value, true);
        return value;
    }

    /**
     * 获取两点间的角度
     * @param p1 点1
     * @param p2 点2
     */
    static getAngle(p1: {x:number, y:number}, p2: {x:number, y:number}): number {
        return Math.atan((p2.y - p1.y) / (p2.x - p1.x));
    }

    /**
     * 获取两点间的距离
     * @param p1 点1
     * @param p2 点2
     */
    static getDistance(p1: { x: number, y: number }, p2: { x: number, y: number }): number {
        return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
    }

    /**
     * 将角度转为弧度
     * @param angle 角度
     */
    static angleToRadian(angle: number): number {
        return angle * Math.PI / 180;
    }

    /**
     * 求旋转后的点坐标
     * @param angle         角度
     * @param point         旋转前的坐标点
     * @param result        
     */
    static getRotationPoint(angle: number, pointOrDir: { x: number, y: number }, result?: { x: number, y: number }): { x: number, y: number } {
        if (result == null) {
            result = { x: 0, y: 0 };
        }
        //角度转弧度
        angle = angle * (Math.PI / 180);
        result.x = Math.cos(angle) * pointOrDir.x - Math.sin(angle) * pointOrDir.y;
        result.y = Math.cos(angle) * pointOrDir.y + Math.sin(angle) * pointOrDir.x;
        return result;
    }

}
